package org.manon.json.asm;

import java.util.ArrayList;
import java.util.List;

import org.manon.json.meta.Entity;
import org.objectweb.asm.ClassWriter;

/**
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 */
final class ASMContextDeSerializer implements ASMContext{

	private final Entity _entity;
	private final ClassWriter _classWriter;
	private final String _className;
	private final ASMClassFactory _holder;
	private final List<ASMAppendToClass> _accessMethods;
	private final List<Class<?>> _associations = new ArrayList<Class<?>>();
	
	private final List<ASMAppendToMethod> _newInstance;
	private final List<ASMAppendToMethod> _appendToConstructor;
	
	public ASMContextDeSerializer(ClassWriter classWriter,Entity entity, ASMClassFactory holder) {
		_classWriter = classWriter;
		_entity = entity;
		_className = entity.getInternalClassNameForDeserializer();
		_holder = holder;
		_accessMethods = new ArrayList<ASMAppendToClass>();
		_newInstance = new ArrayList<ASMAppendToMethod>();
		_appendToConstructor = new ArrayList<ASMAppendToMethod>();
	}
	
	public Entity getEntity() {
		return _entity;
	}
	public String getClassName() {
		return _className;
	}
	public ClassWriter getClassWriter() {
		return _classWriter;
	}
	public ASMClassFactory getHolder() {
		return _holder;
	}
	public void addAccessMethod(ASMAppendToClass accessMethod) {
		_accessMethods.add(accessMethod);
	}
	public List<ASMAppendToClass> getAccessMethods() {
		return _accessMethods;
	}

	public boolean containsDeserializer(Class<?> clazz) {
		return _associations.contains(clazz);
	}
	
	public void addDeserializer(Class<?> clazz) {
		_associations.add(clazz);
	}
	
	public int sizeDeserializers() {
		return _associations.size();
	}
	
	public int indexOfDeserializer(Class<?> clazz) {
		return _associations.indexOf(clazz) +1;
	}

	public void appendNewInstance(ASMAppendToMethod appendToMethod) {
		_newInstance.add(appendToMethod);
	}

	public List<ASMAppendToMethod> getNewInstance() {
		return _newInstance;
	}
	
	public List<ASMAppendToMethod> getAppendToConstructor() {
		return _appendToConstructor;
	}
}